package com.tierstall.client.presenter;

import java.util.ArrayList;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.view.client.ListDataProvider;
import com.tierstall.client.service.WebServices;
import com.tierstall.client.view.content.CV_searchResult;
import com.tierstall.client.view.content.CV_showUsers;
import com.tierstall.shared.Logger;
import com.tierstall.shared.Page;
import com.tierstall.shared.SearchQuery;
import com.tierstall.shared.TierDTO;
import com.tierstall.shared.UserDTO;

public class SearchResultPresenter extends
		AbstractContentViewPresenter<CV_searchResult> implements
		CV_searchResult.Display {

	private ArrayList<TierDTO> userList;
	private ListDataProvider<TierDTO> dataProvider;
	private SearchQuery query;

	public SearchResultPresenter(WebServices rpcServices, String session_id,
			HandlerManager eventBus, MainPresenter mainPresenter,
			UserDTO currentUser, SearchQuery query) {
		super(rpcServices, session_id, eventBus, mainPresenter, currentUser);
		view = (CV_searchResult) mainPresenter
				.getContentView(Page.SEARCH_RESULT);
		this.query = query;
		this.dataProvider = view.getListDataProvider();
		this.view.setPresenter(this);
	}

	@Override
	public void go(HasWidgets container) {
		if (query != null)
			getFilteredResult(query);
		else
			getUnfilteredResult();

	}

	@Override
	public void updateTier(TierDTO tier) {
		// TODO Auto-generated method stub

	}

	@Override
	public void editTier(TierDTO tier) {
		Logger.CLIENT_INFO("Edit Tier POPUP...");

	}

	private void getFilteredResult(SearchQuery query) {
		rpcService.getRpcTierService().queryTiere(sessionId, query,
				new AsyncCallback<ArrayList<TierDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Logger.CLIENT_ERROR("SearchResultPresenter: Failure retrieving tier-list!");

					}

					@Override
					public void onSuccess(ArrayList<TierDTO> result) {
						userList = result;
//						userList = result;
//						dataProvider.setList(userList);
//
//						view.initCellTable(result);
//						dataProvider.addDataDisplay(view.getDataProvider());
//						dataProvider.refresh();
//						// view.onInitialize();
						dataProvider.setList(userList);

						view.initCellTable(result);
						dataProvider.addDataDisplay(view.getDataProvider());
						dataProvider.refresh();
						// view.onInitialize();
						mPresenter.setContentView(view);
					}
				});
	}

	private void getUnfilteredResult() {
		rpcService.getRpcTierService().getTiere(sessionId,
				new AsyncCallback<ArrayList<TierDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Logger.CLIENT_ERROR("SearchResultPresenter: Failure retrieving tier-list!");

					}

					@Override
					public void onSuccess(ArrayList<TierDTO> result) {
						userList = result;

						// userList = new ArrayList<UserDTO>();
						// userList.add(new UserDTO("4123", "username",
						// "password", UserType.ADMIN, "firstname", "lastname",
						// "email", "department"));
						// userList.add(new UserDTO("51234", "username2",
						// "password2", UserType.ADMIN, "firstname2",
						// "lastname2", "email2", "department2"));

						dataProvider.setList(userList);

						view.initCellTable(result);
						dataProvider.addDataDisplay(view.getDataProvider());
						dataProvider.refresh();
						// view.onInitialize();
						mPresenter.setContentView(view);
					}
				});

	}
}
